//
//  FTSliderCell.m
//  WebPDA
//
//  Created by Justin Yip on 9/1/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "FTSliderCell.h"


@implementation FTSliderCell
@synthesize slider;

#pragma mark -
#pragma mark Cell lifecycle

-(id)initWithBundle:(NSBundle*)bundle valueHolder:(id)object reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithBundle:bundle valueHolder:object reuseIdentifier:reuseIdentifier])) 
	{
        // Initialization code
		slider = [[UISlider alloc] initWithFrame:CELL_APPEND_RECT];
		slider.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		
		[self.contentView addSubview:slider];
    }
    return self;
}

-(void)configureWithObject:(id)kvcObject preferenceSpecifier:(NSDictionary*)ps {
	
	[super configureWithObject:kvcObject preferenceSpecifier:ps];
	
	/*
	 note: 必须先设最大最小，再设值
	 */
	if([ps objectForKey:@"MinimumValue"] != nil)
	{
		NSNumber *minValue = [ps objectForKey:@"MinimumValue"];
		slider.minimumValue = [minValue floatValue];
	}
	
	if([ps objectForKey:@"MaximumValue"] != nil)
	{
		NSString *maxValue = [ps objectForKey:@"MaximumValue"];
		slider.maximumValue = [maxValue floatValue]; 
	}
	
	if([ps objectForKey:@"MinimumValueImage"] != nil)
	{
		NSString *minValueImageName = [ps objectForKey:@"MinimumValueImage"];
		slider.minimumValueImage = [UIImage imageNamed:minValueImageName];
	}
	
	if([ps objectForKey:@"MaximumValueImage"] != nil)
	{
		NSString *maxValueImageName = [ps objectForKey:@"MaximumValueImage"];
		slider.maximumValueImage = [UIImage imageNamed:maxValueImageName];
	}
	
	if([ps objectForKey:@"DefaultValue"] != nil)
	{
		NSNumber *defValue = [ps objectForKey:@"DefaultValue"];
		slider.value = [defValue floatValue];
	}
	
	//read value
	NSString *key = [self keyFromPreferenceSpecifier];
	if (key && [kvcObject containsValueForKey:key]) {
		NSNumber *valueNum = [kvcObject valueForKey:key];
		slider.value = [valueNum floatValue];
	}
}

#pragma mark -
#pragma mark Callback

-(id)getValue {
	return [NSNumber numberWithFloat:slider.value];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
	[super setEditing:editing animated:animated];
	slider.enabled = editing;
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[slider removeFromSuperview];
	
	[slider release];
    [super dealloc];
}


@end
